package com.sgb.kjwl.view.ui.activity.manage.special

import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.view.Window
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.utils.TextUtils
import com.blankj.utilcode.util.SizeUtils
import com.gyf.barlibrary.ImmersionBar
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityInviteMemberPostBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.model.entity.manage.special.CompanyEntity
import com.sgb.kjwl.view.event.special.InviteEvent
import com.sgb.kjwl.view.ui.activity.manage.WebSiteBuildBaseActivity
import com.sgb.kjwl.view.ui.adapter.manage.AddViewAdapter
import com.sgb.kjwl.viewmodel.manage.CompanySpecialViewModel
import com.swgk.core.base.di.AppComponent
import com.swgk.core.util.CheckUtils
import com.swgk.core.util.ClickUtils
import com.swgk.core.util.MToast
import com.swgk.core.util.SharedPreferenceUtil
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject

class InviteMemberPostActivity : AppActivity() {

    @Inject
    lateinit var mViewModel : CompanySpecialViewModel

    private lateinit var mBinding : ActivityInviteMemberPostBinding

    private lateinit var  mAddViewAdapter: AddViewAdapter


    private var mEntity : CompanyEntity ?= null
    private var mIsAdd = true

    private var postMap = HashMap<String, Any>()

    companion object{
        private const val KEY_INVITE_ENTITY = "key_invite_entity"
        private const val KEY_IS_ADD = "key_is_add"
        fun launch(context: Context, entity: CompanyEntity, isAdd : Boolean){
            val intent  = Intent(context, InviteMemberPostActivity::class.java)
            intent.putExtra(KEY_INVITE_ENTITY, entity)
            intent.putExtra(KEY_IS_ADD, isAdd)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //解决软键盘挡住recycleView输入框的问题
        val decorView = window.decorView
        val contentView = findViewById<View>(Window.ID_ANDROID_CONTENT)
        val rootView = contentView.findViewById<View>(R.id.ns_scroll_view)
        val lpLl = rootView.layoutParams as LinearLayout.LayoutParams
        decorView.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            decorView.getWindowVisibleDisplayFrame(r)
            val height = decorView.resources.displayMetrics.heightPixels
            val diff = height - r.bottom
            if (diff != 0) {
                if (contentView.paddingBottom != diff) {
                    lpLl.bottomMargin = 0
                }
            } else {
                if (contentView.paddingBottom != 0) {
                    lpLl.bottomMargin = 0
                  }
            }
            rootView.layoutParams = lpLl
        }
//        ImmersionBar.with(this)
//            .statusBarDarkFont(statusBarDark())
//            .fitsSystemWindows(false)
//            .keyboardEnable(false).init()

    }

    override fun initView() {
        super.initView()
        if (intent != null){
            mEntity = intent.getSerializableExtra(KEY_INVITE_ENTITY) as CompanyEntity?
            mIsAdd = intent.getBooleanExtra(KEY_IS_ADD, true)
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_invite_member_post)
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = "邀请成员"
        mAddViewAdapter = AddViewAdapter(this)
        mBinding.addView.setAdapter(mAddViewAdapter)
        mBinding.tvJoinName.text = if (mEntity == null || TextUtils.isEmpty(mEntity!!.companyName)) "" else mEntity!!.companyName
        mBinding.tvInviteName.text = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME, "")
        mBinding.tvCommit.setOnClickListener {
            if (!ClickUtils.isDoubleClick()){
                verificationPost()
            }
        }
        mViewModel.message.observe(this, {
            if (!TextUtils.isEmpty(it)){
                MToast.showToast(this, it)
                EventBus.getDefault().post(InviteEvent.make())
                finish()
            }
        })
    }

    private fun verificationPost() {
        postMap.clear()
        if (TextUtils.isEmpty(mBinding.itvContractName.content)){
            MToast.showToast(this, "请输入联系人")
            return
        }
        postMap["contacts"] = mBinding.itvContractPhone.content
        if (TextUtils.isEmpty(mBinding.itvContractPhone.content)){
            MToast.showToast(this, "请输入联系电话")
            return
        }
        if (!CheckUtils.isPhoneNumberValid(mBinding.itvContractPhone.content)){
            MToast.showToast(this, "请输入正确的联系电话")
            return
        }
        postMap["phone"] = mBinding.itvContractPhone.content
        if (TextUtils.isEmpty(mBinding.cfkDescriptionEdit.editContent)){
            MToast.showToast(this, "请输入300字以内的说明")
            return
        }
        postMap["inviteInfo"] = mBinding.cfkDescriptionEdit.editContent
        postMap["isInvited"] = if (mIsAdd) 0 else 1
        postMap["companyNo"] = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "")
        postMap["companyName"] = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME, "")
        postMap["customFields"] = mBinding.addView.getDate()
        postMap["inviteesContacts"] = if (TextUtils.isEmpty(mEntity!!.concat)) mEntity!!.contacts else mEntity!!.concat
        postMap["inviteesPhone"] =if (TextUtils.isEmpty(mEntity!!.contactNumber)) mEntity!!.phone else mEntity!!.contactNumber
        postMap["inviteesCompanyName"] = mEntity!!.companyName
        postMap["inviteesCompanyNo"] = mEntity!!.companyNo
        if(mIsAdd) mViewModel.inviteAddPost(postMap) else mViewModel.inviteAgainPost(postMap)
    }


}